package com.hw.saoauth.dao;

import cn.dev33.satoken.oauth2.model.SaClientModel;
import com.hw.sacommon.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: sa-token-start
 * @author: hewei
 * @date: 2023-09-12 13:53
 *
 * 模拟提供 Client信息 和 OpenId信息 的数据操作
 **/

@Component
public class SaOauthDao {

    private static final Map<String,SaClientModel> CLIENTS_MAP = new HashMap<>();
    private static final Map<String,String> OPENIDS_MAP = new HashMap<>();

    public static SaClientModel getClient(String clientId) {
        return CLIENTS_MAP.get(clientId);
    }
    public static String getOpenid(String clientId,String loginId) {
        return OPENIDS_MAP.get(clientId +"-"+ loginId);
    }

    @Autowired
    private void initClient() {
        SaClientModel client = new SaClientModel();
        client.setClientId(Constant.CLIENT_ID)
                .setClientSecret(Constant.CLIENT_SECRET)
                .setAllowUrl("*")
                .setContractScope(Constant.CONTRACT_SCOPE)
                .setIsAutoMode(true);
        CLIENTS_MAP.put(Constant.CLIENT_ID,client);

        OPENIDS_MAP.put(Constant.CLIENT_ID +"-"+ Constant.USER_ID,Constant.OPEN_ID);

    }
}
